package hw;


import util.Util;
import util.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Get extends JPanel implements ActionListener {
    private Atm atm;
    private Icon sureImg = Util.getIcon("/buttonSure.jpg");
    private Icon offImg = Util.getIcon("/buttonOff.jpg");
    private JButton sureBtn = new JButton(sureImg);
    private JButton offBtn = new JButton(offImg);
    private JTextField getMoney = new JTextField();

    public Get(Atm atm) {
        this.atm = atm;
        offBtn.setBounds(930,200,140,50);
        sureBtn.setBounds(680,200,140,50);
        getMoney.setBounds(430,200,240,50);
        //添加组件
        this.add(offBtn);
        this.add(sureBtn);
        this.add(getMoney);

        //监听绑定
        offBtn.addActionListener(this);
        sureBtn.addActionListener(this);
        this.setLayout(null);
        this.setOpaque(false);

    }
    @Override
    public void actionPerformed(ActionEvent e) {
        Users use = atm.getUser();
        if (e.getSource() == offBtn) {
            System.out.println("11");
            atm.switchPanel("menu");
        }
        if (e.getSource()==sureBtn){

            if (Integer.parseInt(getMoney.getText())<0&&Integer.parseInt(getMoney.getText())>use.getBalance()){
                JOptionPane.showConfirmDialog(this,"请输入正确的金额");
            }else {
                use.setBalance(use.getBalance()-Integer.parseInt(getMoney.getText()));
                JOptionPane.showConfirmDialog(this,"存款正确");
                AtmApi.inset(use);
            }


        }


    }
}